package com.bailong.nanny.controller;

import com.bailong.nanny.entity.User;
import com.bailong.nanny.service.AddUserService;
import com.bailong.nanny.service.impl.AddUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name= "/addUser.do",urlPatterns = "/addUser.do")
public class AddUserServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String username = req.getParameter("username");
        String realname = req.getParameter("realname");
        String address = req.getParameter("address");
        String tel = req.getParameter("tel");
        User user = new User(0,username,realname,address,tel,"");
//        request.setAttribute("adduser",user);
//        User user = (User) req.getAttribute("adduser");
        if(user!=null){
            AddUserService addUserService = new AddUserServiceImpl();
            int i =addUserService.AddUserDao(user);
            if(i!=0){
                req.getSession().setAttribute("error","添加成功");
                System.out.println("添加成功");
                resp.sendRedirect("/addUser.jsp");

            }else {
                System.out.println("添加失败");
                req.getSession().setAttribute("error","添加失败");
            }
        }else {
            System.out.println("没传进来");
        }

    }
}
